package cn.yuhh.api.v1.declare;

import cn.yuhh.api.helper.ApiResponse;
import cn.yuhh.api.service.AbstractApiService;
import cn.yuhh.core.annotation.NotNull;
import cn.yuhh.core.domain.DeclareInfo;
import cn.yuhh.core.domain.User;
import cn.yuhh.core.type.Role;
import cn.yuhh.core.type.Status;
import cn.yuhh.core.type.Whether;
import com.magic.spring.mongo.Mongo;
import org.bson.types.ObjectId;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Map;

/**
 * @author ysw..
 * @create 2020-02-18 15:55
 * @descreption 签发
 *
 **/
@Component("issue1.0")
@NotNull(name = "id")
public class IssueService extends AbstractApiService {

    @Override
    public Map service(Map request) {
        Whether pass = whether(request);
        DeclareInfo declareInfo = Mongo.buildMongo().eq("_id", new ObjectId(id(request))).findOne(DeclareInfo.class);
        List<User> userList = Mongo.buildMongo().eq("areaInfo.countyCode", declareInfo.getAreaInfo().getCountyCode()).eq("role", Role.HANDLER).eq("status", Status.ACTIVE).find(User.class);
        Mongo.buildMongo().eq("_id",new ObjectId(id(request))).updateFirst(update -> {
            update.set("declareStatus", declareInfo.getDeclareStatus().handler(pass));
            if(!CollectionUtils.isEmpty(userList)){
                update.set("mainManagerId", userList.get(0).getId());
                update.set("otherManagerId", userList.get(1).getId());
            }

            update.set("updateAt", System.currentTimeMillis() / 1000);
            }, DeclareInfo.class);
        //todo 短信通知
        declareLog(id(request));
        return ApiResponse.success();
    }
}